﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Draco.DataStructure
{
    /// <summary>
    /// 顺序表
    /// </summary>
    /// <typeparam name="T">数据类型</typeparam>
    public class D_SeqList<T> : IEnumerable
    {
        private T[] data;
        public int Count { get; set; }

        public D_SeqList(int size)
        {
            data = new T[size];
            Count = 0;
        }

        public D_SeqList() : this(10) { }

        public T this[int index]
        {
            get
            {
                if (index >= 0 && index <= Count - 1)
                {
                    return data[index];
                }
                else
                {
                    //索引不存在
                    return default(T);
                }
            }
        }

        public bool IsEmpty()
        {
            return Count == 0;
        }

        public void Add(T item)
        {
            if (Count == data.Length)
            {
                //表满
            }
            else
            {
                data[Count] = item;
                Count++;
            }
        }

        public void Insert(T item, int index)
        {
            for (int i = Count - 1; i >= index; i--)
            {
                data[i + 1] = data[i];
            }
            data[index] = item;
            Count++;
        }

        public T Delete(int index)
        {
            T temp = data[index];
            for (int i = index+1; i < Count; i++)
            {
                data[i - 1] = data[i];
            }
            Count--;
            return temp;
        }


        public IEnumerator GetEnumerator()
        {
            for (int i = 0; i < Count; i++)
            {
                yield return data[i];
            }
        }
    }
}
